﻿using System.Windows;
using System.Windows.Controls;
using appDataContracts;

namespace appSupplier
{
    /// <summary>
    /// Logique d'interaction pour ShowProductsWindow.xaml
    /// </summary>
    public partial class ShowProductsWindow : Window
    {
        private int catalogId;
        private string catalogName;

        public ShowProductsWindow(int catalogId, string catalogName)
        {
            InitializeComponent();
            this.catalogId = catalogId;
            this.catalogName = catalogName;
            this.Title += catalogName;
            dgProducts.ItemsSource = AppWindow.supplierService.getCatalogProducts(catalogId);
            lblCatalog.Content = "Produits de " + catalogName + " :";
        }

        private void dgProducts_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (dgProducts.SelectedItems.Count > 0)
            {
                btRemoveProduct.Visibility = Visibility.Visible;
            }
            else
            {
                btRemoveProduct.Visibility = Visibility.Hidden;
            }
        }

        private void btRemoveProduct_Click(object sender, RoutedEventArgs e)
        {
            int productId = ((SerializedProduct)dgProducts.SelectedItem).PRODUCTID;
            AppWindow.supplierService.removeProductFromCatalog(productId, catalogId);
            dgProducts.ItemsSource = AppWindow.supplierService.getCatalogProducts(catalogId);
        }

        private void btAddProduct_Click(object sender, RoutedEventArgs e)
        {
            new AddProductWindow(catalogId, catalogName, this).ShowDialog();
        }

        public void update()
        {
            dgProducts.ItemsSource = AppWindow.supplierService.getCatalogProducts(catalogId);
        }

        private void btClose_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}
